package com.springboot.study.tests.collection;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: guodong
 * @Date: 2021/12/22 13:47
 * @Version: 1.0
 * @Description:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
class Fruit {
    public String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Apple extends Fruit {
    public String name;
    public int age;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Orange extends Fruit {
    public String name;
    public int age;
}

public class CovariantArrays {

    public static void main(String[] args) {

        //上界
        List<? extends Fruit> flistTop = new ArrayList<Fruit>();
        flistTop.add(null);
        //add Fruit、Apple对象会报错
//        flistTop.add(new Fruit());
//        flistTop.add(new Apple())
        Fruit fruit1 = flistTop.get(0);

        List<Apple> appleList = new ArrayList<>();
        appleList.add(new Apple("app1", 11));
        print(appleList);





        //下界
        List<? super Fruit> flistBottem = new ArrayList<Fruit>();
        flistBottem.add(new Apple("aooke", 555));
        flistBottem.add(new Orange("orage", 888));
        //get Apple对象会报错
        flistBottem.get(0);
        print2(flistBottem);

    }


    public static void print(List<? extends Fruit> list){
        list.forEach(data ->{
            System.out.println(data);
        });
    }





    public static void print2(List<? super Fruit> list){
        list.forEach(data ->{
            if(data instanceof Apple){
                System.out.println("i am apple:"+ data);

            }
            if(data instanceof Orange){
                System.out.println("i am Orange:"+ data);

            }
        });
    }





}
